package com.bw.clock.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.base.constants.RoutePath
import com.bw.clock.adapter.KaoQinAdapter
import com.bw.clock.databinding.ActivityClockBinding
import com.bw.clock.entity.KaoqinData
import com.bw.clock.net.ClockViewModel
import com.bw.common.adapter.SeleteAdapter
import com.bw.common.entity.SeleteEntity
import com.bw.common.view.BaseMvvmActivity

@Route(path = RoutePath.PATH_CLOCK)
class ClockActivity : BaseMvvmActivity<ActivityClockBinding,ClockViewModel>() {
    lateinit var  kaoQinAdapter: KaoQinAdapter
    lateinit var seleteAdapter: SeleteAdapter
    val newList : ArrayList<KaoqinData> = arrayListOf()
    override fun initView() {
        kaoQinAdapter = KaoQinAdapter()
        seleteAdapter = SeleteAdapter()
        binding.re2.adapter = kaoQinAdapter
        binding.re2.layoutManager = LinearLayoutManager(this)

        binding.back.setOnClickListener {
            finish()
        }
        val itemList = arrayListOf<SeleteEntity>()
        itemList.add(SeleteEntity("全部",true))
        itemList.add(SeleteEntity("1")) // 1 正常
        itemList.add(SeleteEntity("3")) // 3 迟到
        itemList.add(SeleteEntity("2")) // 2 早退
        itemList.add(SeleteEntity("4")) // 4 缺勤
        binding.re1.adapter = seleteAdapter
        binding.re1.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL,false)
        seleteAdapter.data.addAll(itemList)
        seleteAdapter.notifyDataSetChanged()
        seleteAdapter.setOnItemClickListener { adapter, view, position ->
            seleteAdapter.data.forEach {
                it.isSelect = false
            }
            seleteAdapter.data[position].isSelect = true
            seleteAdapter.notifyDataSetChanged()
            val selTitle = seleteAdapter.data[position].title
            if (selTitle == "全部"){  // 有问题
                vm.kaoqin(1)
            } else{
                val filterList = newList.filter { it.status == selTitle }
                kaoQinAdapter.data.clear()
                kaoQinAdapter.data.addAll(filterList)
                kaoQinAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initData() {
        vm.kaoqin(1)
        vm.kaoCg.observe(this){
            kaoQinAdapter.data.addAll(it)
            kaoQinAdapter.data.reverse()
            newList.addAll(it)
            kaoQinAdapter.notifyDataSetChanged()
        }
    }

}